package com.tangcco.lockwell.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Collections;

@Configuration
public class RestTemplateConfig {
    public static String aiUrl = "http://8.130.70.34:8897";
    public static String aiToken = "iJjOTVhZmQ1NTJlZjg0OTZmOGZhZmYyZjczZmNjZD";
    public static String transUrl = "https://fanyi-api.baidu.com/api/trans/vip/translate";

    @Bean
    public RestTemplate aiClient() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(2*60*1000);
        factory.setReadTimeout(2*60*1000);
        
        RestTemplate restTemplate = new RestTemplate(factory);
        
        // 添加拦截器，统一设置请求头
        restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
            HttpHeaders headers = request.getHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("token", aiToken);
            return execution.execute(request, body);
        }));
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }


    @Bean
    public RestTemplate transClient() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(2*60*1000);
        factory.setReadTimeout(2*60*1000);
        RestTemplate restTemplate = new RestTemplate(factory);

        // 添加拦截器，统一设置请求头
        restTemplate.setInterceptors(Collections.singletonList((request, body, execution) -> {
            HttpHeaders headers = request.getHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return execution.execute(request, body);
        }));
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(2*60*1000);
        factory.setReadTimeout(2*60*1000);
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }
} 